package cn.edu.nuc.bre.controller;

import java.util.List;

import org.activiti.engine.identity.Group;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import cn.edu.nuc.bre.entity.TransDetail;
import cn.edu.nuc.bre.entity.TransType;
import cn.edu.nuc.bre.service.interfaces.GroupService;
import cn.edu.nuc.bre.service.interfaces.TransDetailService;
import cn.edu.nuc.bre.service.interfaces.TransTypeService;
import cn.edu.nuc.bre.util.PageInfo;

/**
 * 事务明细Action
 * 
 * @author 王凯
 *
 */
@Controller
@Scope("prototype")
public class TransDetailAction extends ActionSupport {

	/**
	 * 序列化id
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 事务明细业务逻辑对象
	 */
	@Autowired
	private TransDetailService transDetailService;

	/**
	 * 事务类型业务对象
	 */
	@Autowired
	private TransTypeService transTypeService;

	/**
	 * 部门业务对象
	 */
	@Autowired
	private GroupService groupService;

	/**
	 * 当前页
	 */
	private Integer targetPage;

	/**
	 * 事务明细名称（模糊查询条件1）
	 */
	private String transDetailName;

	/**
	 * 事务类型名称（模糊查询条件2）
	 */
	private String transTypeName;

	/**
	 * 事务明细对象
	 */
	private TransDetail transDetail = new TransDetail();

	/**
	 * 分页查询事务明细
	 * 
	 * @return
	 */
	public String listTransDetail() {

		// 如果没有设置页号则默认访问第一页
		if (targetPage == null) {
			targetPage = 1;
		}

		// 查询事务明细
		PageInfo<TransDetail> pageInfo = transDetailService.findByQueryHandler(targetPage, transDetailName, transTypeName);

		// 保存结果集到值栈
		ActionContext.getContext().put("pageInfo", pageInfo);
		ActionContext.getContext().put("transTypeName", transTypeName);
		ActionContext.getContext().put("transDetailName", transDetailName);

		return "success";
	}
	
	/**
	 * 添加事务详情初始化
	 * @return
	 */
	public String initadd() {
		
		// 查询所有未禁用的事务类别
		List<TransType> transTypes = transTypeService.findAll();

		// 查询所有部门（未禁用，但不包含管理员和公告员）
		String[] targetTypes = {"11", "111", "3", "4"};
		List<Group> depts = groupService.findByTypes(targetTypes);
		
		//保存结果集到值栈
		ActionContext.getContext().put("transTypeList", transTypes);
		ActionContext.getContext().put("deptList", depts);

		return "success";
	}
	
	/**
	 * 添加事务详情
	 * @return
	 */
	public String addTransDetail() {
		
		//执行添加操作
		transDetailService.add(transDetail);

		return "success";
	}
	
	/**
	 * 删除事务明细
	 * @return
	 */
	public String deleteTransDetail() {
		
		transDetailService.delete(transDetail.getTdid());
		return SUCCESS;
	}
	
	/**
	 * 初始化修改操作页面的信息
	 * @return
	 */
	public String initModify() {
		
		//查询事务明细的详细信息
		TransDetail transdetail = transDetailService.findById(transDetail.getTdid());
		
		// 查询所有未禁用的事务类别
		List<TransType> transTypes = transTypeService.findAll();

		// 查询所有部门（未禁用，但不包含管理员和公告员）
		String[] targetTypes = {"11", "111", "3", "4"};
		List<Group> depts = groupService.findByTypes(targetTypes);
		
		//保存结果集到值栈
		ActionContext.getContext().put("transTypeList", transTypes);
		ActionContext.getContext().put("deptList", depts);
		ActionContext.getContext().put("transDetail", transdetail);
		
		return "success";
	}
	
	/**
	 * 修改事务明细
	 * @return
	 */
	public String modifyTransDetail() {
		
		transDetailService.update(transDetail);
		return SUCCESS;
	}
	

	/////////////////////////////////////////////////////////////////////////////
	public TransDetail getTransDetail() {
		return transDetail;
	}

	public void setTransDetail(TransDetail transDetail) {
		this.transDetail = transDetail;
	}

	public TransDetailService getTransDetailService() {
		return transDetailService;
	}

	public void setTransDetailService(TransDetailService transDetailService) {
		this.transDetailService = transDetailService;
	}

	public Integer getTargetPage() {
		return targetPage;
	}

	public void setTargetPage(Integer targetPage) {
		this.targetPage = targetPage;
	}

	public String getTransDetailName() {
		return transDetailName;
	}

	public void setTransDetailName(String transDetailName) {
		this.transDetailName = transDetailName;
	}

	public String getTransTypeName() {
		return transTypeName;
	}

	public void setTransTypeName(String transTypeName) {
		this.transTypeName = transTypeName;
	}

	public TransTypeService getTransTypeService() {
		return transTypeService;
	}

	public void setTransTypeService(TransTypeService transTypeService) {
		this.transTypeService = transTypeService;
	}

	public GroupService getGroupService() {
		return groupService;
	}

	public void setGroupService(GroupService groupService) {
		this.groupService = groupService;
	}

}
